package com.mmlc.businessutils;

import android.content.Intent;

import com.mmlc.meimei.lichengkongjian.been.OrderButtonBean;
import com.mmlc.meimei.lichengkongjian.mine.ActivityOrderDetail;
import com.mmlc.meimei.lichengkongjian.mine.ActivityRequestSaleAfter;
import com.mmlc.meimei.lichengkongjian.mine.LogisticsListActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/16 0016.
 */
public class OrderListUtils {

    public OrderButtonBean buttonBean;
    public void doSomething(OrderButtonBean buttonBean){
        this.buttonBean = buttonBean;

        switch (buttonBean.buttonType){
            case 1://取消订单
                CancelOrderRequest request = new CancelOrderRequest(this);
                request.cancelOrder();
                break;
            case 2://去支付
                GoPayRequest goPayRequest = new GoPayRequest(this);
                goPayRequest.goPay();
                break;
            case 3://确认收货
                ConfirmOrderRequest confirmOrderRequest = new ConfirmOrderRequest(this);
                confirmOrderRequest.confirmOrder();
                break;
            case 4:
                break;
            case 5://申请售后
                if(buttonBean.saleAfterFlag){
                    Intent intent = new Intent(buttonBean.activity, ActivityOrderDetail.class);
                    intent.putExtra("orderId",buttonBean.orderId);
                    buttonBean.activity.startActivity(intent);
                }else {
                    Intent intent1 = new Intent(buttonBean.activity, ActivityRequestSaleAfter.class);
                    intent1.putExtra("orderId", buttonBean.orderId);
                    intent1.putExtra("goodsId", buttonBean.goodsId);
                    intent1.putExtra("tip",buttonBean.tip);
                    buttonBean.activity.startActivity(intent1);
                }
                break;
            case 6:
                break;
            case 7://申请退款
                RequestBackMoneyRequest requestBackMoneyRequest = new RequestBackMoneyRequest(this);
                requestBackMoneyRequest.refund(buttonBean.orderId);
                break;
            case 8://查看物流
                Intent intent = new Intent(buttonBean.activity, LogisticsListActivity.class);
                intent.putExtra("orderId",buttonBean.orderId);
                buttonBean.activity.startActivity(intent);
                break;
            case 9://删除订单
                DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest(this);
                deleteOrderRequest.deleteOrder();
                break;
        }
    }
    public static List<Integer> getOper(Object obj){
        List<Integer> orderButtonType = new ArrayList<Integer>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int j = 0; j < fields.length; j++) {
            try {
                fields[j].setAccessible(true);
                int value  = fields[j].getInt(obj);
                if(value == 1){
                    int type = getType(fields[j].getName());
                    if(type != -1){
                        orderButtonType.add(type);
                    }
                }
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
        }
        return orderButtonType;
    }

    public static int getType(String name) {
        if(name.equals("cancel")){//取消订单 type = 1;
            return 1;
        }
        if(name.equals("go_pay")){//去支付 type = 2;
            return 2;
        }
        if(name.equals("confirm")){//确认收货 type = 3;
            return 3;
        }
        if(name.equals("comment")){//评价晒单 type = 4 //暂时不提供
            return 4;
        }
        if(name.equals("sale_after")){//申请售后 type = 5
            return 5;
        }
        if(name.equals("buy_again")){//再次购买 type = 6 //暂时不提供
            return 6;
        }
        if(name.equals("refund")){//申请退款 type = 7
            return 7;
        }
        if(name.equals("logistics_trace")){//查看物流 type = 8
            return 8;
        }
        if(name.equals("delete_order")){//删除订单 type = 9
            return 9;
        }
        return -1;
    }
    public static String[] getStringFromType(int type) {
        String[] strings = new String[2];

        switch (type){
            case 1:
                strings[0] = "取消订单";
                strings[1] = "1";
                break;
            case 2:
                strings[0] = "去支付";
                strings[1] = "2";
                break;
            case 3:
                strings[0] = "确认收货";
                strings[1] = "3";
                break;
            case 4:
                strings[0] = "评价晒单";
                strings[1] = "4";
                break;
            case 5:
                strings[0] = "申请售后";
                strings[1] = "5";
                break;
            case 6:
                strings[0] = "再次购买";
                strings[1] = "6";
                break;
            case 7:
                strings[0] = "申请退款";
                strings[1] = "7";
                break;
            case 8:
                strings[0] = "查看物流";
                strings[1] = "8";
                break;
            case 9:
                strings[0] = "删除订单";
                strings[1] = "9";
                break;
        }
        return strings;
    }
    public interface UpdateUI{
        public static int REMOVE_ITEM = 1;
        public static int FINISH = 2;
        public static int REFRESH = 3;
        /**type=1(移除某个项目)*/
        public void updateUI(int position,int type);
    }
    public UpdateUI u;
    public void setUpdateUI(UpdateUI u){
        this.u = u;
    }
}
